package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressMapper;
import com.sky.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Override
    public void addAddress(AddressBook addressInfo) {
        addressInfo.setUserId(BaseContext.getCurrentId());
        addressInfo.setIsDefault(StatusConstant.DISABLE);

        addressMapper.insertAddress(addressInfo);
    }

    @Override
    public List<AddressBook> getUserAddressList(AddressBook addressInfo) {
        List<AddressBook> addressBookList = addressMapper.selectAddressList(addressInfo);

        return addressBookList;
    }

    @Override
    public AddressBook selectById(Long addressId) {
        AddressBook addressInfo = addressMapper.selectById(addressId);

        return addressInfo;
    }

    @Override
    public void updateAddressInfo(AddressBook addressInfo) {
        addressMapper.updateAddressInfo(addressInfo);
    }

    @Override
    public void updateDefaultAddress(AddressBook addressInfo) {
        // 先将该用户所有地址修改为非默认
        addressMapper.updateUserAllAddressToNotDefault(addressInfo);

        // 再将指定的改为默认地址
        addressInfo.setIsDefault(StatusConstant.ENABLE);
        addressMapper.updateAddressInfo(addressInfo);
    }

    @Override
    public void deleteAddress(Long addressId) {
        addressMapper.deleteAddress(addressId);
    }
}
